package guilayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import controllayer.OrderCtrl;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private JPanel jpnlLeftMenu;
	private JPanel jpnlTop;
	private JPanel jpnlMainContent;
	private JPanel jpnlBottom;
	private JLabel lblLoggetIndSom;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setTitle("Vestbjerg Byggecenter A/S - Administrativt system");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		/*Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int width = (int)screenSize.getWidth();
		int height = (int)screenSize.getHeight();*/
		//setBounds(100, 100, width, height);
		setBounds(100,100,1250,700);
		//contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		startLogin();
	}

	private void startLogin() {
		jpnlLeftMenu = null;//new LeftMenu(0);
		jpnlTop = new MainWindowTopPanel();
		jpnlMainContent = new MainLogin();//new MainContent(0, null);
		jpnlBottom = getBottomPanel(false);
		
		setThisContentPane();
	}
	
	public void logout() {
		OrderCtrl.getInstance().logoutAndClearClerk();
		startLogin();
	}

	private void setThisContentPane() {
		contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout(0, 0));
		if (jpnlLeftMenu!=null) {
			contentPane.add(jpnlLeftMenu, BorderLayout.WEST);
		}
		contentPane.add(jpnlTop, BorderLayout.NORTH);
		contentPane.add(jpnlMainContent, BorderLayout.CENTER);
		contentPane.add(jpnlBottom, BorderLayout.SOUTH);
		setContentPane(contentPane);
		
		this.setVisible(true);
	}
	
	public void switchLeftMenu(int menuItemIndex)
	{
		jpnlLeftMenu = new LeftMenu(menuItemIndex);
		setThisContentPane();
	}
	
	public void promptMsg(String msg)
	{
		System.out.println(msg);
	}
	
	public void refreshAll(int leftMenu, int mainContent, Object data) {
		jpnlLeftMenu = new LeftMenu(leftMenu);
		jpnlTop = new MainWindowTopPanel();
		jpnlMainContent = new MainContent(mainContent, data);
		jpnlBottom = getBottomPanel(true);
		//contentPane.updateUI();
		contentPane.removeAll();
		//this.setVisible(false);
		setThisContentPane();
	}
	
	private JPanel getBottomPanel(boolean loggedIn) {
		JPanel bottomPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) bottomPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		if (loggedIn) {
			lblLoggetIndSom = new JLabel("Logget ind som "+ OrderCtrl.getInstance().getClerkName());
			bottomPanel.add(lblLoggetIndSom);
		
			JButton btnNewButton = new JButton("Logud!");
			btnNewButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					logout();
				}
			});
			bottomPanel.add(btnNewButton);
		}
		
		return bottomPanel;
	}

}
